#!/usr/bin/env bash
set -e # 这告诉bash一但有任何一个语句返回非真的值，则退出bash。

PROG_NAME=`basename $0`

function print_help {
    echo -e "Usage: $PROG_NAME [options] pid"
    echo -e ""
    echo -e "Options:"
    echo -e "  -h, --help"
    echo -e "    Displays help message."
    echo -e ""
    echo -e "  -s, --simple"
    echo -e "    Displays in simple mode."
    echo -e ""
}

CMD_GETOPT=`getopt -o sh --long simple,help -n 'example.bash' -- "$@"`
eval set -- "$CMD_GETOPT"
while true ; do
    case "$1" in
        -h|--help) print_help; exit 1 ;;
        -s|--simple) SIMPLE_MODE=1; shift ;;
        --) shift; break ;;
        *) echo "Unknown option: $1"; print_help; exit 1 ;;
    esac
done

for arg do
    TMP_EXE_PATH=`readlink "/proc/$arg/exe"`
    if [[ -n $SIMPLE_MODE ]] ; then
        echo "$TMP_EXE_PATH"
    else
        echo "$arg $TMP_EXE_PATH"
    fi
done

